package org.ghost.quickstart.scalac.loop

import java.text.MessageFormat

import org.ghost.quickstart.scalac.traits.BaseTrait

object ForLoop extends BaseTrait {
  override def print(): Unit = {
    //1 to 10
    println("*****测试 1 to 10")
    for (a <- 1 to 10) {
      println("1 to 10 is [1,10] a: " + a)
    }

    //1 until 10
    println("*****测试 1 until 10")
    for (b <- 1 until 10) {
      println("1 until 10 is [1,10) b: " + b)
    }

    //双重循环
    println("*****测试双重循环")
    for (a <- 1 to 10; b <- 1 until 10) {
      println(MessageFormat.format("a={0},b={1}", String.valueOf(a), String.valueOf(b)))
    }

    //集合循环
    println("*****集合循环")
    var list = List(1, 10, 54, 65, 14, 24, 2, 8, 9)
    for (a <- list) {
      println("list -> a:" + a)
    }

    //集合循环过滤
    println("*****集合循环过滤")
    for (a <- list
         if a % 2 == 0 && a < 20) {
      println("filter list -> a:" + a)
    }
    for (a <- list; b <- 1 until 10
         if a % 2 == 0 && a < 20 && b % 6 == 5) {
      println(MessageFormat.format("双重循环filter list -> (a:{0},b:{1})", String.valueOf(a), String.valueOf(b)))
    }

    println("*****for循环带返回值")
    var retVal1 = for {a <- list
                       if a != 3; if a < 8
    } yield a
    println("for循环带返回值:" + retVal1)
    var retVal2 = for {a <- list; b <- 1 until 10
                       if a != 3; if a % 3 == 1 && (a + b) % 6 == 5 && a < b
    } yield a
    println("for循环带返回值:" + retVal2)
  }
}
